package rs.actor

import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.{ActorContext, Behaviors}
import com.typesafe.scalalogging.Logger
import rs.dev.I2cDev

import scala.concurrent.{ExecutionContext, Future}
import rs.dev.I2cDJ

object Hand {
  def apply(): Behavior[HandCommand] = {
    Behaviors.setup[HandCommand]( ctx => new Hand(ctx).ready())
  }
}
class Hand(ctx: ActorContext[HandCommand]) {

  given ctx1: ActorContext[HandCommand] = ctx
  given executionContext :ExecutionContext = ctx.executionContext

  val log = Logger(getClass)

  var claw = new I2cDJ(12, 60, 50, 100)
  val base = new I2cDJ(13, 90, 0, 180)
  val handA = new I2cDJ(14, 90, 30, 130)
  val handB = new I2cDJ(15, 20, 0, 50)

  val baseAcc = 2
  val clowAcc = 2
  val armAcc = 1

  def ready(): Behavior[HandCommand] = Behaviors.receive { (ctx, msg: HandCommand) =>
    log.debug(s"get hand command : ${msg}")
    given context: ActorContext[HandCommand] = ctx
    msg match {
      case HandBaseTurn(direction) => base.startRotate( Math.signum(direction)*baseAcc)
      case HandClaw(option) => claw.startRotate(Math.signum(option)* clowAcc)
      case HandA(velocity) => handA.startRotate(velocity * armAcc)
      case HandB(velocity) => handB.startRotate(velocity * armAcc)
    }
    Behaviors.same
  }
}


